package com.dmw.service.impl;

import com.dmw.dao.DmLinkUserDao;
import com.dmw.dao.DmUserDao;
import com.dmw.entity.DmLinkUser;
import com.dmw.service.DmUserService;
import com.dmw.util.MD5;
import com.dmw.util.RedisUtil;
import com.dmw.vo.placeOrderVO.TicketBuyerListOfUserIdVO;
import com.dmw.vo.userInfoPageVO.ReturnUserInfoTokenVO;
import com.dmw.vo.userLoginVO.PasswordLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 用户表(DmUser)表服务实现类
 *
 * @author yuzhuangzhuang
 * @since 2021-03-26 14:24:53
 */
@Service("dmUserService")
public class DmUserServiceImpl implements DmUserService {
    @Resource
    private DmUserDao userMapper;
    @Resource
    private DmLinkUserDao linkMapper;
    @Resource
    private RedisUtil redisUtil;


//    //激活码前缀
    private String active_prefix = "active:";


    @Override
    public List<PasswordLoginVO> login(Map<String, Object> map) throws Exception  {
        return userMapper.login(map);
    }


    @Override
    public int checkEmail(String phone) throws Exception{
           return userMapper.checkEmail(phone);
    }

    @Override
    public String register(Map<String, Object> map) throws Exception{

        /* 判断, key是否存在 */
//        if (!redisUtil.exists(active_prefix + map.get("phone"))) return "1002";
        /* 比对激活码 */
        //获取redis中的激活码
        String value = redisUtil.getString(active_prefix + map.get("phone"));
        Long may = userMapper.checkId(map.get("phone").toString());
        if(may!=null) return "1002";
        if (map.get("vcode").equals(value)) {
            //添加用户账号
            map.put("password",MD5.getMd5(map.get("password").toString(),32));
            Date d=new Date();
            map.put("createdTime",d);
            userMapper.register(map);
            Long id = userMapper.checkId(map.get("phone").toString());
            map.clear();
            map.put("targetId",id);
            map.put("createdTime",d);
            userMapper.addImg(map);
            return "0000";
        }
        return "1004";
    }

    @Override
    public List<ReturnUserInfoTokenVO> queryUserInfoByToken(Long userId) throws Exception {
        return userMapper.queryUserInfoByToken(userId);
    }

    @Override
    public void modifyUserInfo(Map<String, Object> map) throws Exception {
        userMapper.modifyUserInfo(map);
    }

    @Override
    public List<TicketBuyerListOfUserIdVO> queryLinkUser(Integer userId) throws Exception {
        return linkMapper.queryLinkUser(userId);
    }

    @Override
    public void addLinkUser(DmLinkUser user) throws Exception {
        linkMapper.addLinkUser(user);
    }

    @Override
    public int queryLinkUserByIdCard(Map<String, Object> map) throws Exception {
        return linkMapper.queryLinkUserByIdCard(map);
    }

    @Override
    public void deleteLinkUserById(Long linkId) throws Exception {
        linkMapper.deleteLinkUserById(linkId);
    }
}
